/**
 * This class allows players to save or load instances of their game.
 * 
 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
 * @version 1.0
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
 * @version 1.0
 */
public class SaveLoad {

	/**
	 * Field file.
	 */
	private File file;

	/**
	 * Field gui.
	 */
	protected SpaceTraderGui gui = null;

	/**
	 * Field writer
	 */
	protected ObjectOutputStream writer = null;

	/**
	 * Field saveFile
	 */
	protected FileOutputStream saveFileOut = null;

	/**
	 * Field saveFileIn
	 */
	protected FileInputStream saveFileIn = null;

	/**
	 * Field reader
	 */
	protected ObjectInputStream reader = null;

	/**
	 * toString method.

	 * @return String */
	public String toString() {
		return "";
	}

	/**
	 * Constructor for the saving and loading object.
	 * 
	 * @param gui
	 *            The gui to save and load.
	 */
	public SaveLoad(SpaceTraderGui gui) {
		this.gui = gui;
	}

	/**
	 * Save the game to a file Structure of save file:
	 * 
	 */
	public void saveGame() {
		if (file != null) {
			final boolean deleted = file.delete();
			System.out.println(deleted);
		}
		try {
			file = new File("save.sav");
			this.saveFileOut = new FileOutputStream(file);
			this.writer = new ObjectOutputStream(saveFileOut);
			writer.writeObject(this.gui.getPlayer());
			writer.writeObject(this.gui.getCurrentSystem());
			writer.writeObject(this.gui.getUniverse());
			writer.writeObject(this.gui.getMarketplace().getBuyPrices());
			writer.writeObject(this.gui.getMarketplace().getSellPrices());
		} catch (IOException e) {
			System.out.println("Could not save file");
			System.out.println(e.getMessage());
		} finally {
			try {
				writer.close();
				saveFileOut.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Loads a saved instance of the game.
	 */
	public void loadGame() {
		try {
			this.saveFileIn = new FileInputStream("save.sav");
			this.reader = new ObjectInputStream(saveFileIn);
			final Player player = (Player) reader.readObject();
			final SolarSystem currentSystem = (SolarSystem) reader.readObject();
			final List<SolarSystem> universe = (ArrayList<SolarSystem>) reader
					.readObject();
			final int[] buyPrices = (int[]) reader.readObject();
			final int[] sellPrices = (int[]) reader.readObject();

			gui.loadData(player, currentSystem, universe, buyPrices, sellPrices);
		} catch (Exception e) {
			System.out.println("No save file exists");
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				saveFileIn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
